{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example Q8: Realistic Two Qubit Tuneup and Experiments\n",
    "This example notebook shows how to use APS2/X6 ecosystem to tune up a pair of qubits.\n",
    "\n",
    "© Raytheon BBN Technologies 2019"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ['AWG_DIR'] = \"./AWG\"\n",
    "\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import QGL.config\n",
    "import auspex.config\n",
    "auspex.config.AWGDir = \"/home/qlab/BBN/AWG\"\n",
    "QGL.config.AWGDir = \"/home/qlab/BBN/AWG\"\n",
    "auspex.config.KernelDir = \"/home/qlab/BBN/Kernels\"\n",
    "QGL.config.KernelDir = \"/home/qlab/BBN/Kernels\"\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "from auspex.analysis.qubit_fits import *\n",
    "from auspex.analysis.helpers import *\n",
    "\n",
    "from QGL import *\n",
    "from auspex.qubit import *\n",
    "\n",
    "#import seaborn as sns\n",
    "#sns.set_style('whitegrid')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Channel Library Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this will all be system dependent\n",
    "cl = ChannelLibrary(db_resource_name=\":memory:\")\n",
    "\n",
    "q1 = cl.new_qubit(\"q1\")\n",
    "q2 = cl.new_qubit(\"q2\")\n",
    "ip_addresses = [f\"192.168.4.{i}\" for i in [21, 22, 23, 24, 25, 26, 28, 29]]\n",
    "aps2 = cl.new_APS2_rack(\"Maxwell\", ip_addresses, tdm_ip=\"192.168.4.11\")\n",
    "cl.set_master(aps2.px(\"TDM\"))\n",
    "dig_1  = cl.new_X6(\"MyX6\", address=0)\n",
    "\n",
    "dig_1.record_length = 4096\n",
    "\n",
    "# qubit 1\n",
    "AM1 = cl.new_source(\"AutodyneM1\", \"HolzworthHS9000\", \"HS9004A-492-1\", \n",
    "                     power=16.0, frequency=6.4762e9, reference=\"10MHz\")\n",
    "\n",
    "q1src = cl.new_source(\"q1source\", \"HolzworthHS9000\", \"HS9004A-492-2\", \n",
    "                     power=16.0, frequency=4.2e9, reference=\"10MHz\")\n",
    "\n",
    "cl.set_measure(q1, aps2.tx(4), dig_1.channels[1], gate=False, trig_channel=aps2.tx(6).ch(\"m3\"), generator=AM1)\n",
    "cl.set_control(q1, aps2.tx(5), generator=q1src)\n",
    "\n",
    "\n",
    "cl[\"q1\"].measure_chan.autodyne_freq = 11e6\n",
    "cl[\"q1\"].measure_chan.pulse_params = {\"length\": 3e-6,\n",
    "                                      \"amp\": 1.0,\n",
    "                                      \"sigma\": 1.0e-8,\n",
    "                                      \"shape_fun\": \"tanh\"}\n",
    "\n",
    "\n",
    "cl[\"q1\"].frequency = 67.0e6\n",
    "cl[\"q1\"].pulse_params = {\"length\": 100e-9,\n",
    "                         \"pi2Amp\": 0.4,\n",
    "                         \"piAmp\": 0.8,\n",
    "                         \"shape_fun\": \"drag\",\n",
    "                         \"drag_scaling\": 0.0,\n",
    "                         \"sigma\": 5.0e-9}\n",
    "\n",
    "#qubit 2\n",
    "AM2 = cl.new_source(\"AutodyneM2\", \"HolzworthHS9000\", \"HS9004A-492-3\", \n",
    "                     power=16.0, frequency=6.4762e9, reference=\"10MHz\")\n",
    "\n",
    "q2src = cl.new_source(\"q2source\", \"HolzworthHS9000\", \"HS9004A-492-4\", \n",
    "                     power=16.0, frequency=4.2e9, reference=\"10MHz\")\n",
    "\n",
    "cl.set_measure(q2, aps2.tx(7), dig_1.channels[0], gate=False, trig_channel=aps2.tx(6).ch(\"m3\"), generator=AM2)\n",
    "cl.set_control(q2, aps2.tx(8), generator=q2src)\n",
    "\n",
    "cl[\"q2\"].measure_chan.autodyne_freq = 11e6\n",
    "cl[\"q2\"].measure_chan.pulse_params = {\"length\": 3e-6,\n",
    "                                      \"amp\": 1.0,\n",
    "                                      \"sigma\": 1.0e-8,\n",
    "                                      \"shape_fun\": \"tanh\"}\n",
    "\n",
    "cl.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize all four APS2 to linear regime\n",
    "for i in range(4,8):\n",
    "    aps2.tx(i).ch(1).I_channel_amp_factor = 0.5\n",
    "    aps2.tx(i).ch(1).Q_channel_amp_factor = 0.5\n",
    "    aps2.tx(i).ch(1).amp_factor = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pl = PipelineManager()\n",
    "pl.create_default_pipeline(qubits=[q1,q2])\n",
    "\n",
    "for ql in ['q1','q2']:\n",
    "    qb = cl[ql]\n",
    "    pl[ql].clear_pipeline()\n",
    "\n",
    "    pl[ql].stream_type = \"raw\"\n",
    "\n",
    "    pl[ql].create_default_pipeline(buffers=False)\n",
    "    \n",
    "    pl[ql][\"Demodulate\"].frequency = qb.measure_chan.autodyne_freq\n",
    "\n",
    "    # only enable this filter when you're running single shot fidelity\n",
    "    #pl[ql].add(FidelityKernel(save_kernel=True, logistic_regression=True, set_threshold=True, label=\"Q1_SSF\"))\n",
    "\n",
    "    pl[ql][\"Demodulate\"][\"Integrate\"].box_car_start = 3e-7\n",
    "    pl[ql][\"Demodulate\"][\"Integrate\"].box_car_stop = 2.3e-6\n",
    "\n",
    "    #pl[ql].add(Display(label=ql+\" - Raw\", plot_dims=0))\n",
    "    #pl[ql][\"Demodulate\"].add(Display(label=ql+\" - Demod\", plot_dims=0))\n",
    "    pl[ql][\"Demodulate\"][\"Integrate\"][\"Average\"].add(Display(label=ql+\" - Final Average\", plot_dims=0))\n",
    "    \n",
    "    # if you want to see partial averages: \n",
    "    #pl[ql][\"Demodulate\"][\"Integrate\"][\"Average\"].add(Display(label=ql+\" - Partial Average\", plot_dims=0), connector_out=\"partial_average\")\n",
    "    \n",
    "    #pl[ql][\"Demodulate\"][\"Integrate\"][\"Average\"].add(Display(label=ql+\" - Partial Average1d\", plot_dims=1), connector_out=\"partial_average\")\n",
    "pl.show_pipeline()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cavity Spectroscopy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pf = PulsedSpec(q1, specOn=False)\n",
    "plot_pulse_files(pf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp = QubitExperiment(pf,averages=256)\n",
    "#exp.add_qubit_sweep(q2, \"measure\", \"frequency\", np.linspace(6.38e9, 6.395e9, 51))\n",
    "exp.add_qubit_sweep(q1, \"measure\", \"frequency\", np.linspace(6.424e9, 6.432e9, 45))\n",
    "#exp.add_qubit_sweep(q1,\"measure\",\"amplitude\",np.linspace(0.2,0.8,10))\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data, desc = exp.writers[0].get_data()\n",
    "# plt.plot(desc.axes[0].points, np.abs(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "AM1.frequency = 6.42843e9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "AM2.frequency = 6.3863e9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Qubit Spectroscopy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qb = q1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qb.frequency = 0.0\n",
    "qb.pulse_params['length'] = 5e-6\n",
    "qb.pulse_params['shape_fun'] = \"tanh\"\n",
    "pf = PulsedSpec(qb, specOn=True)\n",
    "plot_pulse_files(pf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pf = PulsedSpec(qb, specOn=True)\n",
    "exp = QubitExperiment(pf,averages=256)\n",
    "exp.add_qubit_sweep(qb, \"control\", \"frequency\", np.linspace(5.28e9, 5.34e9, 61))\n",
    "#exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data, desc = exp.writers[0].get_data()\n",
    "# plt.plot(desc.axes[0].points, np.abs(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q1.frequency = -63e6\n",
    "q1.pulse_params['length'] = 200e-9\n",
    "q1.pulse_params['shape_fun'] = \"tanh\"\n",
    "q1.pulse_params['piAmp'] = 0.4\n",
    "q1.pulse_params['pi2Amp'] = 0.2\n",
    "pf = PulsedSpec(q1, specOn=True)\n",
    "#plot_pulse_files(pf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fq = 5.26e9 #5.2525e9 \n",
    "q1src.frequency = fq - q1.frequency\n",
    "q1.phys_chan.I_channel_amp_factor = 0.2\n",
    "q1.phys_chan.Q_channel_amp_factor = 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q1src.frequency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q2.frequency = 81e6\n",
    "q2.pulse_params['length'] = 200e-9\n",
    "q2.pulse_params['shape_fun'] = \"tanh\"\n",
    "q2.pulse_params['piAmp'] = 0.4\n",
    "q2.pulse_params['pi2Amp'] = 0.2\n",
    "pf = PulsedSpec(q2, specOn=True)\n",
    "#plot_pulse_files(pf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fq2 = 5.2113e9\n",
    "q2src.frequency = fq2 - q2.frequency\n",
    "q2.phys_chan.I_channel_amp_factor = 0.2\n",
    "q2.phys_chan.Q_channel_amp_factor = 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q2src.frequency"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mixer calibration "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "salo = cl.new_source(\"salo\", \"HolzworthHS9000\", \"HS9004A-381-4\", \n",
    "                     power=10.0, frequency=6.5e9, reference=\"10MHz\")\n",
    "specAn = cl.new_spectrum_analzyer('specAn','ASRL/dev/ttyACM0',salo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from auspex.instruments import enumerate_visa_instruments, SpectrumAnalyzer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "enumerate_visa_instruments()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# from here out, uncomment cal.calibrate() if you want to actually run the cal\n",
    "cal = MixerCalibration(q1,specAn,'control', phase_range = (-0.5, 0.5), amp_range=(0.8, 1.2))\n",
    "#cal.calibrate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# listed here only if manual adjustment is needed\n",
    "\n",
    "q1.phys_chan.I_channel_offset = -0.0004\n",
    "q1.phys_chan.Q_channel_offset = -0.019\n",
    "q1.phys_chan.amp_factor = 1.004\n",
    "q1.phys_chan.phase_skew = 0.053"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "cal = MixerCalibration(q2,specAn,'control', phase_range = (-0.5, 0.5), amp_range=(0.8, 1.2))\n",
    "#cal.calibrate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q2.phys_chan.I_channel_offset = -0.0004\n",
    "q2.phys_chan.Q_channel_offset = -0.019\n",
    "q2.phys_chan.amp_factor = 0.985\n",
    "q2.phys_chan.phase_skew = 0.074"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cal = MixerCalibration(q1,specAn,'measure', phase_range = (-0.5, 0.5), amp_range=(0.8, 1.2))\n",
    "#cal.calibrate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cal = MixerCalibration(q2,specAn,'measure', phase_range = (-0.5, 0.5), amp_range=(0.8, 1.2))\n",
    "#cal.calibrate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rabi Width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pf = RabiWidth(q1, np.arange(20e-9, 0.602e-6, 10e-9))\n",
    "exp = QubitExperiment(pf, averages=200)\n",
    "plot_pulse_files(pf)\n",
    "#exp.add_qubit_sweep(q1, \"control\", \"frequency\", q1src.frequency + np.linspace(-6e6, 6e6, 61))\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pf = RabiWidth(q2, np.arange(20e-9, 0.602e-6, 10e-9))\n",
    "exp = QubitExperiment(pf, averages=200)\n",
    "plot_pulse_files(pf)\n",
    "#exp.add_qubit_sweep(q2, \"control\", \"frequency\", q2src.frequency + np.linspace(-2e6, 2e6, 21))\n",
    "#exp.add_qubit_sweep(q2, \"measure\", \"frequency\", AM2.frequency + np.linspace(-2e6, 2e6, 11))\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rabi Amp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pf = RabiAmp(q1, np.linspace(-1, 1, 101))\n",
    "exp = QubitExperiment(pf, averages=128)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cal = RabiAmpCalibration(q1,quad='imag')\n",
    "#cal.calibrate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q1.pulse_params['piAmp'] = 0.6179\n",
    "q1.pulse_params['pi2Amp'] = q1.pulse_params['piAmp']/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pf = RabiAmp(q2, np.linspace(-1, 1, 101))\n",
    "exp = QubitExperiment(pf, averages=128)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cal = RabiAmpCalibration(q2,quad='imag')\n",
    "#cal.calibrate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q2.pulse_params['piAmp'] = 0.743\n",
    "q2.pulse_params['pi2Amp'] = q2.pulse_params['piAmp']/2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ramsey Calibration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# need to be in the neighbourhood for this to work\n",
    "\n",
    "cal = RamseyCalibration(q1,quad='imag')\n",
    "#cal.calibrate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## T1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qb = q2\n",
    "icpts = np.linspace(20e-9, 201.02e-6, 101)\n",
    "pf = InversionRecovery(qb, icpts)\n",
    "exp = QubitExperiment(pf, averages=400)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data, desc = exp.writers[0].get_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from auspex.analysis.qubit_fits import T1Fit, RamseyFit\n",
    "from auspex.analysis.helpers import cal_scale"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sdata = cal_scale(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fit = T1Fit(icpts, abs(data[0:-4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fit.make_plots()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### T2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "rpts = np.linspace(20e-9, 50.02e-6, 101)\n",
    "pf = Ramsey(q1, rpts ,TPPIFreq=0e3)\n",
    "#exp.add_qubit_sweep(q1, \"control\", \"frequency\", q1src.frequency + np.linspace(-2e6, 2e6, 21))\n",
    "exp = QubitExperiment(pf, averages=200)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data, desc = exp.writers[0].get_data()\n",
    "sdata = cal_scale(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fit = RamseyFit(rpts, abs(data[0:-4]),make_plots = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fcorrect = fit.fit_params['f']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fcorrect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#q1src.frequency -= fcorrect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pf = Ramsey(q2, rpts,TPPIFreq=0e3)\n",
    "exp = QubitExperiment(pf, averages=200)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data, desc = exp.writers[0].get_data()\n",
    "sdata = cal_scale(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fit = RamseyFit(rpts, abs(data[0:-4]),make_plots = True)\n",
    "fcorrect = fit.fit_params['f']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fcorrect*1e-6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#q2src.frequency += fcorrect"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Echo experiments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp = QubitExperiment(HahnEcho(q2, np.linspace(20e-9, 80.02e-6, 81), periods=5), averages=512)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data, desc = exp.writers[0].get_data()\n",
    "cdata = cal_scale(np.real(data))\n",
    "fit = RamseyFit(desc.axes[0].points[:-4], cdata, make_plots=True)\n",
    "fit.fit_params"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single Qubit Cals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "RabiAmpCalibration(q1, quad=\"imag\").calibrate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PiCalibration(q1, quad=\"imag\", num_pulses=7).calibrate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Pi2Calibration(q1, quad=\"imag\", num_pulses=7).calibrate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single-Qubit RB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from auspex.analysis.qubit_fits import *\n",
    "from auspex.analysis.helpers import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rb_lens = [2, 4, 8, 16, 32, 128, 256, 512]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rb_seqs = create_RB_seqs(1, rb_lens)\n",
    "pf = SingleQubitRB(q1, rb_seqs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp = QubitExperiment(pf, averages=256)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data, desc = exp.writers[0].get_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lens = [int(l) for l in desc.axes[0].points[:-4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SingleQubitRBFit(lens, cal_scale(np.imag(data)), make_plots=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fancier things"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Maybe you want to see how $T_1$ varies with repeated measurement..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from auspex.parameter import IntParameter\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 1000\n",
    "lengths = np.linspace(20e-9, 500.02e-6, 101)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T1seq = [[X(q2), Id(q2, length=d), MEAS(q2)] for d in lengths]\n",
    "T1seq += create_cal_seqs((q2,), 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wait_param = IntParameter(default=0)\n",
    "wait_param.name = \"Repeat\"\n",
    "#wait = lambda x: print(f\"{x}\")\n",
    "def wait(x):\n",
    "    print(f\"{x}\")\n",
    "    time.sleep(2)\n",
    "wait_param.assign_method(wait)\n",
    "\n",
    "\n",
    "mf = compile_to_hardware(T1seq, \"T1/T1\")\n",
    "exp = QubitExperiment(mf, averages=512)\n",
    "exp.wait_param = wait_param\n",
    "# with these params each shot is roughly 22 secs apart\n",
    "\n",
    "exp.add_sweep(exp.wait_param, list(range(N))) # repeat T1 scan 1000 times\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load data\n",
    "# get T1s\n",
    "T1s = []\n",
    "T1_error = []\n",
    "#data, desc = exp.writers[0].get_data()\n",
    "for i in range(N):\n",
    "    cdata = cal_scale(np.imag(data[i,:]))\n",
    "    fit = T1Fit(lengths, cdata, make_plots=False)\n",
    "    T1s.append(fit.fit_params[\"T1\"])\n",
    "    T1_error.append(fit.fit_errors[\"T1\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(8,6))\n",
    "#plt.errorbar(range(1000),np.array(T1s)*1e6, yerr=np.array(T1_error)*1e6, fmt='+', elinewidth=0.5, barsabove=True, capsize=0.7)\n",
    "plt.plot(range(1000),np.array(T1s)*1e6, '+')\n",
    "plt.title(r'Q2 $T_1$ Variability')\n",
    "plt.xlabel('N repeats')\n",
    "plt.ylabel(r'$T_1$ (us)')\n",
    "#plt.savefig('T1vsTime.png', dpi=300, bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2Q RB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lengths = [2,4,6,8,10] # range(2,10) #[2**n for n in range(1,6)]\n",
    "seqs = create_RB_seqs(2, lengths=lengths)\n",
    "exp = qef.create(TwoQubitRB(q1, q2, seqs=seqs), expname='Q2Q1RB')\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2Q Gates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "edge12 = cl.new_edge(q1,q2)\n",
    "cl.set_control(edge12, aps2.tx(5), generator=q1src)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q1_10 = q1.phys_chan.generator.frequency + q1.frequency\n",
    "q2_10 = q2.phys_chan.generator.frequency + q2.frequency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "edge12.frequency = q2_10 - q1.phys_chan.generator.frequency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "edge12.pulse_params = {'length': 400e-9, 'amp': 0.8, 'shape_fun': 'tanh', 'sigma':10e-9}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "q1.measure_chan.pulse_params['amp'] = 1.0\n",
    "q2.measure_chan.pulse_params['amp'] = 1.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CR length cal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "crlens = np.arange(100e-9,2.1e-6,100e-9)\n",
    "pf = EchoCRLen(q1,q2,lengths=crlens)\n",
    "plot_pulse_files(pf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp = QubitExperiment(pf, averages=512)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Above just runs the experiment used by the calibration routine.  Here is the actual calibration:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "crlens = np.arange(100e-9,2.1e-6,100e-9)\n",
    "# phase, amp and rise_fall have defaults but you can overwrite them\n",
    "pce = CRLenCalibration(cl[\"q1->q2\"], lengths=lengths, phase = 0, amp = 0.8, rise_fall = 40e-9,\n",
    "            do_plotting=True, sample_name=\"CRLen\", averages=512)\n",
    "pec.calibrate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CR phase cal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "phases = np.arange(0,np.pi*2,np.pi/20)\n",
    "pf = EchoCRPhase(q1,q2,phases,length=1000e-9)\n",
    "plot_pulse_files(pf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp = QubitExperiment(pf, averages=512)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "phases = np.linspace(0, 2*np.pi, 21)\n",
    "pce = CRPhaseCalibration(edge12, phases = phases, amp = 0.8, rise_fall = 40e-9,\n",
    "            do_plotting=True, sample_name=\"CRPhase\", averages=512)\n",
    "pec.calibrate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CR amp cal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "amps = np.arange(0.7,0.9,0.1)\n",
    "pf = EchoCRAmp(q1,q2,amps,length=1000e-9)\n",
    "plot_pulse_files(pf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp = QubitExperiment(pf, averages=512)\n",
    "exp.run_sweeps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pce = CRAmpCalibration(cl[\"q1->q2\"], amp_range = 0.2, rise_fall = 40e-9,\n",
    "            do_plotting=True, sample_name=\"CRAmp\", averages=512)\n",
    "pec.calibrate()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "latex_envs": {
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 0
  },
  "nav_menu": {},
  "toc": {
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 6,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
